package com.ukao.pad;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ukao.pad.base.BaseFragment;
import com.ukao.pad.consts.SaveParamets;
import com.ukao.pad.ui.LoginActivity;
import com.ukao.pad.ui.enterFactory.StartFactoryFragment;
import com.ukao.pad.ui.leaveFactory.LeaveFactoryFragment;
import com.ukao.pad.ui.myData.MyDataTabFragment;
import com.ukao.pad.ui.orders.CreateOrdersFragment;
import com.ukao.pad.ui.packPackag.packPackagFragment;
import com.ukao.pad.ui.clothingQuery.ClothingInformationFragment;
import com.ukao.pad.ui.recheck.RecheckFragment;
import com.ukao.pad.ui.setting.SettingFragment;
import com.ukao.pad.ui.stokin.StockinFragment;
import com.ukao.pad.utils.T;
import com.ukao.pad.widget.StateImageView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.ukao.pad.R.id.storage_btn;

/**
 * 根Fragment
 */
public class HomeFragment extends BaseFragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private Unbinder unbinder;
    @BindView(R.id.start_work_btn)
    StateImageView startWorkBtn;
    @BindView(R.id.end_work_btn)
    StateImageView endWorkBtn;
    @BindView(R.id.storage_btn)
    StateImageView storageBtn;
    @BindView(R.id.mydata_btn)
    StateImageView myDataBtn;
    @BindView(R.id.storage_layout)
    LinearLayout storageLayout;
    @BindView(R.id.setting_btn)
    StateImageView settingBtn;
    @BindView(R.id.item_home_fragment_itemname)
    TextView itemHomeFragmentItemname;
    @BindView(R.id.query_btn)
    StateImageView queryBtn;
    @BindView(R.id.iv_orders_btn)
    StateImageView  ordersBtn;
    private String mParam1;
    private String mParam2;
    private List<String> list;
    private List<BaseFragment> fragments = new ArrayList<>();
    private View view;

    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initAdapter();
        initListener();
    }

    @Override
    protected void initView() {
        //获得资源文件里面的String 数组
        list = Arrays.asList(getActivity().getResources().getStringArray(R.array.home_item));
        //我的数据
        fragments.add(MyDataTabFragment.newInstance("",""));
        //设置Fragment
        fragments.add(SettingFragment.newInstance(0,""));
    }

    /**
     * 用户可见的时候  就是可以交互的时候
     */
    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        DisplayMetrics metric = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;         // 屏幕宽度（像素）
        int height = metric.heightPixels;       // 屏幕高度（像素）
        float density = metric.density;         // 屏幕密度（0.75 / 1.0 / 1.5）
        int screenWidth = (int) (width / density);  // 屏幕宽度(dp)
        int screenHeight = (int) (height / density);// 屏幕高度(dp)
        Log.d("h_bl", "屏幕宽度（像素）：" + width);
        Log.d("h_bl", "屏幕高度（像素）：" + height);
        Log.d("h_bl", "屏幕密度（0.75 / 1.0 / 1.5）：" + density);
        Log.d("h_bl", "屏幕密度dpi（120 / 160 / 240）：" + metric.densityDpi);
        Log.d("h_bl", "屏幕宽度（dp）：" + screenWidth);
        Log.d("h_bl", "屏幕高度（dp）：" + screenHeight);
        int val = SaveParamets.getVal();
//        if (val == 2) {
//            storageLayout.setVisibility(View.VISIBLE);
//        } else {
//            storageLayout.setVisibility(View.GONE);
//        }
    }

    //    private   OnItemClickListener  mOnItemClickListener=new MyOnItemClickListener() {
//
//        @Override
//        public void onItemClick(ViewGroup viewGroup, View view, Object o, int i) {
//            super.onItemClick(viewGroup, view, o, i);
//
//            switch (i){
//                case  0 :
//                    start(MyDataTabFragment.newInstance("",""));
//                    break;
//                case 1:
//                       start(SettingFragment.newInstance(0,""));
//                    break;
//
//                case  2 :/*入厂*/
//
//                    start(StartFactoryFragment.newInstance("",""));
//                    break;
//                case  3:/*出厂*/
//
//                    start(LeaveFactoryFragment.newInstance(null,""));
//                    break;
//                case  4 : /*复检*/
//
//                    start(RecheckFragment.newInstance("",""));
//                    break;
//                case  5 ://打包
//
//                    start(packPackagFragment.newInstance("",""));
//                    break;
//                case  6 ://会员卡
//
//                    start(WriteVipCardFragment.newInstance("",""));
//                    break;
//                case  7 ://写标签卡
//
//                    start(WriteLabelFragment.newInstance("",""));
//                    break;
//            }
//
//        }
//    };
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
    /**
     * 启动Wifi界面
     */
    public void startWifiFragment() {
        start(SettingFragment.newInstance(1, ""));

    }

    @OnClick({R.id.start_work_btn, R.id.end_work_btn, R.id.mydata_btn, R.id.setting_btn, storage_btn,R.id.query_btn,R.id.iv_orders_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.start_work_btn:  //开始工作
                int val = SaveParamets.getVal();
                switch (val) {
                    case 1: //入厂
                        start(StartFactoryFragment.newInstance("", ""));
                        break;
                    case 2:
                        start(RecheckFragment.newInstance("", ""));
                        break;
                    case 3:  /*其他的一些工位*/
                        start(packPackagFragment.newInstance(false, ""));
                        break;
                    case 4:  /*质检打包*/
                        start(packPackagFragment.newInstance(true, ""));
                        break;
                    case 5:  /*出厂*/
                        start(LeaveFactoryFragment.newInstance(null, ""));
                        break;
                    case -1:
                        T.show("请设置工位");
                        break;
                }
                break;
            case R.id.end_work_btn:
                SaveParamets.setIsLogin(false);
                Intent intent = new Intent(_mActivity, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
            case storage_btn:   //入库
                start(StockinFragment.newInstance(""));
                break;
            case R.id.mydata_btn:   //我的数据
                start(MyDataTabFragment.newInstance("", ""));
                break;
            case R.id.setting_btn:   //设置
                start(SettingFragment.newInstance(0, ""));
                break;
            case R.id.query_btn:  //衣物查询
               start(ClothingInformationFragment.newInstance("", ""));
                break;
            case R.id.iv_orders_btn:   //下单
                start(CreateOrdersFragment.newInstance("",""));
                break;

        }
    }


}
